package com.lifeng.test;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by lifeng on 2019/8/20.
 */
public class FTPUtil {

    public static void download() throws IOException {
        FTPClient ftpClient = new FTPClient();
        ftpClient.setControlEncoding("UTF-8");
        ftpClient.connect("192.168.31.21",21);
        ftpClient.login("ftp_test", "123456");
        int replyCode = ftpClient.getReplyCode();
        ftpClient.setDataTimeout(120000);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//设置为二进制文件

        if (!FTPReply.isPositiveCompletion(replyCode)) {
            ftpClient.disconnect();
            System.out.println("FTP连接失败");
        }else {
            System.out.println("FTP连接成功");
        }
        InputStream in = ftpClient.retrieveFileStream("test/123");
        List<String> lines = IOUtils.readLines(in);
        //关闭连接
        if (ftpClient != null) {
            ftpClient.logout();
            ftpClient.disconnect();
        }
    }

    public static void main(String[] args) throws IOException {
        download();
    }
}
